
//Get the dimenions of an image that will cause it to scale to fit within a placeholder.
function scaleToFit(settings) {
	var objImage = new Image();	
	
	//Use an Image object in order to get the actual dimensions of the image.				
	objImage.onload = function() {
		var imageWidth, imageHeight, ratioX, ratioY, scale, newWidth, newHeight;
		
		imageWidth = objImage.width;
		imageHeight = objImage.height;
					
		if ((imageWidth > 0) && (imageHeight > 0)) {
			//Calculate scale ratios.
			ratioX = settings.rsW / imageWidth;
			ratioY = settings.rsH / imageHeight;
			scale = ratioX < ratioY ? ratioX : ratioY;

			//Calculate and set new image dimensions.
			newWidth = parseInt(imageWidth * scale, 10);
			newHeight = parseInt(imageHeight * scale, 10);						
		}

		//Call the callback function and pass the new dimensions.
		settings.callback(newWidth, newHeight);
	}
	
	//Call the error handler if the image could not be loaded.
	objImage.onerror = function() {
		settings.onerror(objImage);
	}

	objImage.setAttribute("src", settings.url);
}

function loadScrollingScript(swipeEnabled, callback) {
    var js;
    var overscroll = "http://risegadgets.googlecode.com/svn/test/Sponsors/jquery.overscroll.js";
    var jScrollPane = "http://risegadgets.googlecode.com/svn/test/common/jScrollPane/script/jquery.jscrollpane.min.js";
    var jScrollPaneMouseWheel = "http://risegadgets.googlecode.com/svn/test/common/jScrollPane/script/jquery.mousewheel.js";
    
    //Load the Overscroll library for swiping.
    if (swipeEnabled) {
	js = new Array(overscroll);
    }
    //Load the jScrollPane library for scrolling.
    else {
	js = new Array(jScrollPaneMouseWheel, jScrollPane);
    }
    
    this.loadScripts(js, callback);
}

function loadScripts(js, callback) {
    var scriptsLoaded = 0;
    var script = null;
    var head = document.getElementsByTagName("head")[0];
    
    //Load JavaScript.
    for (var i = 0; i < js.length; i++) {
	script = document.createElement("script");
	script.type = "text/javascript";
	script.onload = function() {
	    scriptsLoaded++;
	    
	    //When the last script file has loaded, execute the callback function, if applicable.
	    if (scriptsLoaded == js.length) {
		if (callback) {
		    callback();
		}
	    }
	};
	    
	script.src = js[i];
	head.appendChild(script);
    }
}